<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const arr = [
            {'id': '29', 'pid': '', 'name': '总裁办'},
            {'id': '2c', 'pid': '', 'name': '财务部'},
            {'id': '2d', 'pid': '2c', 'name': '财务核算部'},
            {'id': '2f', 'pid': '2c', 'name': '薪资管理部'},
            {'id': 'd2', 'pid': '', 'name': '技术部'},
            {'id': 'd3', 'pid': 'd2', 'name': 'Java研发部'},
        ]
       function transformListtoTree(parms){
        const obj = {}

        parms.forEach((item) => {
            // console.log(item.pid);
            // console.log(item.children);
            if(!item.children){
                item.children = []
            }
            obj[item.id] = item
        })
        console.log(obj);
        console.log(arr);
        const newArr = []

        parms.forEach(item => {
            const parent = obj[item.pid]
            console.log(parent);
            if(parent){
                parent.children.push(item)
            }else{
                newArr.push(item)
            }
        })
        console.log(newArr);
       }
       transformListtoTree(arr)



        // arr.forEach(item => {
        //     if(item.pid){
        //         item.children = []
        //     }
        // })
        // console.log(arr);
        // arr.forEach((item,index) => {
        //     // console.log(index);
        //     const newArr = []
        //     if(item.children){
        //         newArr.push(item)
        //     }else{

        //     }
        // })


    </script>
</body>
</html>